/*
 * Copyright (c) 2015  3 9
 * ---3 :5:54
 * ---作者:吴培基
 * ---QQ:757671834
 */

package kali.wpj.kali.scan;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.TextView;
import android.widget.Toast;

import com.github.johnpersano.supertoasts.SuperToast;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.marshalchen.common.commonUtils.urlUtils.HttpUtilsAsync;
import com.marshalchen.common.ui.ToastUtil;

import org.apache.http.Header;

import java.io.UnsupportedEncodingException;

import butterknife.ButterKnife;
import butterknife.InjectView;
import info.hoang8f.widget.FButton;
import kali.wpj.kali.MainActivity;
import kali.wpj.kali.R;
import kali.wpj.kali.common.settingActivity;
import kali.wpj.kali.globe.ServiceInfo;
import kali.wpj.kali.util.IPTools;
import kali.wpj.kali.util.SharedTools;


public class TchSshDosActivity extends ActionBarActivity {
    @InjectView(R.id.btn_ssl_start)
    FButton btn_ssl_start;
    @InjectView(R.id.txt_ssl_result)
    TextView txt_ssl_result;
    @InjectView(R.id.Np_ssl_times)
    NumberPicker Np_ssl_times;
    @InjectView(R.id.ed_tch_ip)
     EditText ed_tch_ip;

    private Dialog mDialog;
    private String URLCENTER= ServiceInfo.TCH;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tch_ssh_dos);
        ButterKnife.inject(this);
        init();

        btn_ssl_start.setOnClickListener(ssl_OnclickerListener);
    }

    /**
     *发送数据点击事件
     */
    View.OnClickListener ssl_OnclickerListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {

               if(!IPTools.haveHostIP(TchSshDosActivity.this)){
                Toast.makeText(TchSshDosActivity.this,"请先设置服务器IP",Toast.LENGTH_SHORT).show();
                startActivity(new Intent(TchSshDosActivity.this, settingActivity.class));
                TchSshDosActivity.this.finish();
            }else{
                 int time=Np_ssl_times.getValue();
                 String serviceIP=  SharedTools.getString(TchSshDosActivity.this,"ip");
                 String targetIP=ed_tch_ip.getText().toString().trim();
                 if(targetIP==null||targetIP+""==""){
                     SuperToast superToast = new SuperToast(TchSshDosActivity.this);
                     superToast.setDuration(SuperToast.Duration.LONG);
                     superToast.setText(getString(R.string.inputIP));
                     superToast.setIcon(SuperToast.Icon.Dark.INFO, SuperToast.IconPosition.LEFT);
                     superToast.show();
                     return ;
                 }
                  String shell="targetIP/"+time;
                  getResult(serviceIP,shell);
               }

        }
    };
    /**
     * @see初始化NumberPicker
     */
             public void  init(){
                    Np_ssl_times.setMinValue(100);
                    Np_ssl_times.setMaxValue(1000);
                    Np_ssl_times.setValue(400);
                }

    /**
     *
     * @param Ip 服务器的IP地址
     * @param shell 命令
     */
    private  void getResult(String Ip,String shell){
        HttpUtilsAsync httpUtilsAsync=new HttpUtilsAsync();
        httpUtilsAsync.setTimeout(99999);
        //http://
        httpUtilsAsync.get("http://" + Ip + URLCENTER + shell, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                System.out.println("成功了" + bytes.toString());
                String str = null;
                try {
                    str = new String(bytes, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                txt_ssl_result.setText(str);
            }

            @Override
            public void onFinish() {
                mDialog.dismiss();

            }

            @Override
            public void onStart() {
                showRoundProcessDialog(TchSshDosActivity.this, R.layout.loading_process_dialog_anim);

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                SuperToast superToast = new SuperToast(TchSshDosActivity.this);
                superToast.setDuration(SuperToast.Duration.LONG);
                superToast.setText(getString(R.string.tchfail));
                superToast.setIcon(SuperToast.Icon.Dark.INFO, SuperToast.IconPosition.LEFT);
                superToast.show();
            }
        });

    }


    public void showRoundProcessDialog(Context mContext, int layout){
        mDialog = new AlertDialog.Builder(mContext).create();
        mDialog.setTitle("努力查询中...");
        mDialog.show();
        // 注意此处要放在show之后 否则会报异常
        mDialog.setContentView(layout);
    }
    @Override
    public void onBackPressed() {
        startActivity(new Intent(TchSshDosActivity.this, MainActivity.class));
        overridePendingTransition(R.anim.ac_transition_disappear_bottom_right_in,R.anim.ac_transition_disappear_bottom_right_out);
        this.finish();
    }

}
